package org.test;
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// JListTEst.java
//An example of the JList component in action. This program uses a custom
//renderer (BookCellRenderer.java) to show a list of books with icons of their
//covers.
//

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

import org.aftputil.*;

public class JListTest1 extends JPanel {

  private AftpFileItem books[] = 
  {
		  new AftpFileItemWin("drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysql "),
		  new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 0 Jul 06 03:29 mysasdfql "),
		  new AftpFileItemWin("drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysasdfdsaffql "),
		  new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 0 Jul 06 03:29 mysdsasdfql "),
		  new AftpFileItemWin("drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysql "),
		  new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 0 Jul 06 03:29 mysasdfql "),
		  new AftpFileItemWin("drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysasdfdsaffql "),
		  new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 0 Jul 06 03:29 mysdsasdfql "),
		  new AftpFileItemWin("drw-rw-rw- 1 ftp admin 0 Jul 06 03:19 mysql "),
		  new AftpFileItemWin("-rw-rw-rw- 1 ftp admin 0 Jul 06 03:29 mysasdfql "),
		 
  };

  private JList booklist = new JList(books);

  public JListTest1() {
    setLayout(new BorderLayout());
    setSize(600,400);
    JButton button = new JButton("Print");
    button.addActionListener(new PrintListener());

    booklist = new JList(books);
    booklist.setCellRenderer(new AftpListCellRenderer());
    booklist.setVisibleRowCount(4);
    JScrollPane pane = new JScrollPane(booklist);

    add(pane, BorderLayout.NORTH);
    add(button, BorderLayout.SOUTH);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new JListTest1());
    frame.pack();
    frame.setVisible(true);
  }

  // An inner class to respond to clicks on the Print button
  class PrintListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      int selected[] = booklist.getSelectedIndices();
      System.out.println("Selected Elements:  ");

      for (int i = 0; i < selected.length; i++) {
    	  AftpFileItem element = (AftpFileItem) booklist.getModel()
            .getElementAt(selected[i]);
        System.out.println("  " + element.getName());
      }
    }
  }
}
